﻿using AutoMapper;
using B.S.Business.Domain;
using B.S.Business.ErrorCode;
using B.S.Business.Infacetrue;
using B.S.Business.Write.API.Applications.Command;
using MediatR;
using Yitter.IdGenerator;

namespace B.S.Business.Write.API.Applications.CommandHandler
{
    public class PurchaseUpdCommandHandler : IRequestHandler<PurchaseUpdCommand, ApiResult<object>>
    {
        private readonly IBaseRepository<Purchase> purchaseRepo;
        private readonly IMapper mapper;

        public PurchaseUpdCommandHandler(IBaseRepository<Purchase> purchaseRepo, IMapper mapper)
        {
            this.purchaseRepo = purchaseRepo;
            this.mapper = mapper;
        }
        /// <summary>
        /// 采购更新处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<object>> Handle(PurchaseUpdCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> result = new ApiResult<object>();
            var m = mapper.Map<Purchase>(request);

            m.ProductName = request.ProductName;
            m.Productcode = YitIdHelper.NextId().ToString();
            m.Specification = request.Specification;
            m.UnitName = request.UnitName;
            m.Univalence = request.Univalence;
            m.Quantity = request.Quantity;
            m.ExtendedPrice = request.ExtendedPrice;

            var a = purchaseRepo.Update(m);
            if (a > 0)
            {
                result.Code = ApiCode.修改成功;
                result.Msg = "修改成功";
                result.Data = a;

            }
            else
            {
                result.Code = ApiCode.修改成功;
                result.Msg = "修改成功";
                result.Data = a;
            }
            return Task.FromResult(result);
        }
    }

}
